import {
    Component,
    Injector,
    ViewEncapsulation,
    ViewChild,
    ElementRef,
    OnInit,
} from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    ComboboxItemDto,
    OrganizationUnitServiceProxy,
    UserWorkingTimeServiceProxy,
    GetOrgCharDto,
} from '@shared/service-proxies/service-proxies';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import * as moment from 'moment';
import { Table } from 'primeng/table';
import { Paginator } from 'primeng/primeng';

@Component({
    templateUrl: './workingchar.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()],
})
export class WorkingcharComponent extends AppComponentBase implements OnInit {
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;

    public startbDate: moment.Moment = moment().startOf("day");
    public endbDate: moment.Moment = moment().endOf("day");
    public startuDate: moment.Moment = moment().startOf("day");
    public enduDate: moment.Moment = moment().endOf("day");
    orgid: ComboboxItemDto;
    teamGroups: ComboboxItemDto[] = [];
    data: any;
    orgData = new GetOrgCharDto();
    userData = new GetOrgCharDto();
    userdata: any;

    constructor(
        injector: Injector,
        private _organizationUnitServiceProxy: OrganizationUnitServiceProxy,
        private _userWorkingTimeServiceProxy: UserWorkingTimeServiceProxy
    ) {
        super(injector);
    }

    ngOnInit() {
        this._organizationUnitServiceProxy
            .getTeamGroups()
            .subscribe((result) => {
                this.teamGroups = result.items;
                this.orgid = this.teamGroups[0];
                this.getWorking();
            });
    }
    getWorking() {
        this.primengTableHelper.showLoadingIndicator();
        // let start = null;
        // let end = null;
        // if (this.dateBRange !== undefined) {
        //     start = moment(this.dateBRange[0]);
        //     end = moment(this.dateBRange[1]);
        // }
        this._userWorkingTimeServiceProxy
            .getOrgWorkingChar(this.startbDate,
                this.endbDate)
            .subscribe((result) => {
                this.orgData = result;
                this.data = {
                    labels: this.orgData.orgNames,
                    datasets: [
                        {
                            backgroundColor: [
                                '#FF6384',
                                '#36A2EB',
                                '#FFCE56',
                                '#c020ff',
                                '#9CCC65',
                                '#ffa0ff',
                                '#FF3300',
                                '#42A5F5',
                            ],
                            data: this.orgData.dataLists,
                        },
                    ],
                };
                this.primengTableHelper.hideLoadingIndicator();
            });
    }
    getUserChar() {
        this.primengTableHelper.showLoadingIndicator();
        // let start = null;
        // let end = null;
        // if (this.dateURange !== undefined) {
        //     start = moment(this.dateURange[0]);
        //     end = moment(this.dateURange[1]);
        // }
        //console.log(start);
        if (this.orgid.displayText != '请选择班组') {
            this._userWorkingTimeServiceProxy
                .getUserWorkingChar(this.startuDate,
                    this.enduDate, this.orgid.displayText)
                .subscribe((result) => {
                    this.userData = result;
                    this.userdata = {
                        labels: this.userData.orgNames,
                        datasets: [
                            {
                                label: '个人工时',
                                backgroundColor: '#9CCC65',
                                borderColor: '#7CB342',
                                data: this.userData.dataLists,
                            },
                        ],
                    };
                    this.primengTableHelper.hideLoadingIndicator();
                });
        } else {
            this.primengTableHelper.hideLoadingIndicator();
            this.userdata = null;
        }
    }
    cleanB() {
        // this.dateBRange = undefined;
        this.getWorking();
    }
    cleanU() {
        // this.dateURange = undefined;
        this.orgid = this.teamGroups[0];
        this.getUserChar();
    }
}
